import uviewPlus from "uview-plus";
import formatMoney from "@/utils/formatMoney";


import {createSSRApp} from 'vue'
import App from './App.vue'
//全局空状态
import empty from "@/components/empty"
//vuex
import store from '@/store/index.js';
import * as mainFn from '@/utils/mainFn'
// 引入全局方法
// import globalMethods from '@/static/js/globalMethods.js';
import CryptoJS from 'crypto-js'
import getLoction from "@/utils/getLoction";


export function createApp() {
    const app = createSSRApp(App)
    app.component('empty', empty)

    // 配置全局分享
    app.mixin({
        onLoad(options) {
            // 保存页面参数，供分享时使用
            if(options.shareID){
                uni.setStorageSync('shareID', options.shareID)
            }
        },

        onShareAppMessage() {
            // 携带参数：from（来源页面）和可能的其他参数
            const user=uni.getStorageSync('userInfo')
            const sharePath = `/pages/index/index?shareID=${user.uid}`

            return {
                title: store.state.name,
                path: sharePath,
            }
        },
        onShareTimeline() {
            const user=uni.getStorageSync('userInfo')
            const sharePath = `/pages/index/index?shareID=${user.uid}`

            return {
                title: store.state.name,
                path: sharePath,
            }
        }
    })

    // 全局取消打印
    if (process.env.NODE_ENV == 'production') {
        console.log = () => {
        };
    }
    app.use(uviewPlus);

    console.log("版本:", process.env.NODE_ENV)
    // 项目配置
    app.config.globalProperties.$store = store
    app.config.globalProperties.$template = store.state.template
    app.config.globalProperties.$defaultAvatar = store.state.defaultAvatar
    app.config.globalProperties.$baseurl = store.state.baseurl
    app.config.globalProperties.$share = store.state.share
    app.config.globalProperties.$SHOPbaseurl = store.state.SHOPbaseurl
    app.config.globalProperties.$CryptoJS = CryptoJS
    app.config.globalProperties.$formatMoney = formatMoney;

    // #ifdef H5
    // 设置favicon
    const favicon = document.createElement('link')
    favicon.rel = 'shortcut icon'
    favicon.href = store.state.logo // 替换为你的图标地址
    document.head.appendChild(favicon)
    // #endif

    app.config.globalProperties.$setPhone = mainFn.setPhone
    app.config.globalProperties.$md5Encrypt = mainFn.md5Encrypt
    // 字符串转换为Base64
    app.config.globalProperties.$stringToBase64 = mainFn.stringToBase64
    // 判断是否在微信浏览器   true是
    app.config.globalProperties.$isWeiXinBrowser = mainFn.isWeiXinBrowser
    app.config.globalProperties.$decrypt = mainFn.decrypt

    // 注册$showPrompt到Vue原型上，以方便全局调用
    app.config.globalProperties.$showPrompt = mainFn.showPrompt
    // 定义全局方法
    app.config.globalProperties.$goURl = mainFn.goURl
    // 定义装修跳转全局方法
    app.config.globalProperties.$gotosUrls = mainFn.gotosUrls
    app.config.globalProperties.$payList = (str) => {
        let is_h5
        // #ifdef APP-PLUS
        is_h5 = 0
        // #endif
        // #ifdef H5
        if (app.config.globalProperties.$isWeiXinBrowser()) {
            is_h5 = 3
        } else {
            is_h5 = 1
        }
        // #endif
        // #ifdef MP-WEIXIN
        is_h5 = 2
        // #endif
        app.config.globalProperties.$request({
            url: `api/payType/?is_h5=${is_h5}`,
            method: 'GET',
            success(res) {
                if (res.errno == 0) {
                    console.log("res", res)
                    let list = res.data.sort((a, b) => {
                        return Number(b.sort) - Number(a.sort)
                    })
                    uni.setStorageSync('payType', list)
                }
            }
        })
    }
    app.config.globalProperties.$evalPro = (str) => { //已废弃
        var Fn = Function;
        return new Fn('return ' + str)();
    }
    app.config.globalProperties.$toFiexd = mainFn.toFiexd
    app.config.globalProperties.$clearTime = mainFn.clearTime
    app.config.globalProperties.$toast =mainFn.toast
    app.config.globalProperties.$isIos = mainFn.isIos
    app.config.globalProperties.$isAuto = mainFn.isAuto
    app.config.globalProperties.$sellPrice = mainFn.sellPrice

    app.config.globalProperties.$userInfo = mainFn.userInfo

    app.config.globalProperties.$Uplode = mainFn.Uplode
    app.config.globalProperties.$UplodeVideo = mainFn.UplodeVideo
    app.config.globalProperties.$requestPromise = mainFn.requestPromise
    app.config.globalProperties.$request = mainFn.request
    app.config.globalProperties.$SHOPrequest = mainFn.requestHop
    // 使用全局方法
    // app.use(globalMethods);
    // 引入vuex
    app.use(store)
    return {
        app
    }
}

